<template>
  <div>
    <div style="text-align: left;margin-left:2em">
      <el-button type="text" @click="addProjectCustomer" style="margin-right:20px" size="medium" :disabled="disableEditSubmit || disableSave" v-show="!(disableEditSubmit || disableSave)"><i class="el-icon-circle-plus-outline"></i>新增客户决策信息</el-button>
    </div>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row v-for="(item, index) in model.projectCustomerList" :key="index">
        <el-col :span="12">
          <el-button type="text" @click="deleteProjectCustomer(index)" size="medium" v-show="!(disableEditSubmit || disableSave)" style="float: left;padding-top: 6px"><i class="el-icon-remove-outline"></i>删除</el-button>
          <el-form-item label="填写时间" :prop="'projectCustomerList.' + index + '.finishwritetime'" label-width="100px" style="margin-left: 50px">
            <el-date-picker v-model="item.finishwritetime" type="date" value-format="yyyy-MM-dd" :disabled="true"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户决策链支持度" :prop="'projectCustomerList.' + index + '.clientsupport'" :rules="rules.validateRequire">
            <el-input v-model="item.clientsupport"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="客户决策模式" :prop="'projectCustomerList.' + index + '.decisionmode'" :rules="rules.validateRequire">
            <el-input v-model="item.decisionmode" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    return {
      moduleTitle: '项目立项-客户决策',
      urlPrefix: '/mpm/projectCustomerDecision/',
      url: {
        loadCustomerInfo: '/mpm/projectCustomerDecision/loadCustomerInfo'
        // 额外的url
      },
      disableSave: true
    }
  },
  props: {
    projectid: {
      type: Number,
      default: 0
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    open() {
      this.$set(this.model, 'projectCustomerList', [{ finishwritetime: new Date() }])
      let that = this
      that.$nextTick(() => {
        that.$refs.form.clearValidate()
        if (that.isAdd === true) {
          that.loaded()
        } else if (that.disableEditSubmit === false) {
          that.title = '编辑' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.loaded()
          })
        } else {
          that.title = '查看' + that.moduleTitle
          that.queryData(that.projectid).then((_) => {
            that.$refs.form.clearValidate()
            that.loaded()
          })
        }
      })
    },
    queryData(projectid) {
      if (!this.url.loadCustomerInfo) {
        this.$tap.alert('请设置loadCustomerInfo地址!', 'warning')
        return
      }
      return getAction(this.url.loadCustomerInfo, { projectid: projectid }).then((res) => {
        if (res.success) {
          // 不显示空的客户决策信息
          let customer = res.result.filter((item) => item.clientsupport || item.decisionmode)
          this.$set(this.model, 'projectCustomerList', customer)
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // 删除客户决策信息
    deleteProjectCustomer(index) {
      if (this.model.projectCustomerList.length === 1) {
        this.$tap.alert('客户决策信息数量为1时，不允许删除')
      } else {
        this.model.projectCustomerList.splice(index, 1)
      }
    },
    // 添加挂网招标日期
    addProjectCustomer() {
      this.model.projectCustomerList.push({
        projectid: this.projectid,
        finishwritetime: new Date()
      })
    },
    validateCustomerDecision() {
      let that = this
      if (that.model.projectCustomerList.length === 0) {
        return false
      }
      for (let i = 0; i < that.model.projectCustomerList.length; i++) {
        let bid = that.model.projectCustomerList[i]
        if (bid.clientsupport === null || bid.clientsupport === undefined || bid.clientsupport === '' || bid.decisionmode === null || bid.decisionmode === undefined || bid.decisionmode === '') {
          return false
        }
      }
      return true
    }
  }
}
</script>
